package com.shiguiwu.springmybatis.javabase.thread;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @description: ScheduleThreadPoolExecutor
 * @author: stone
 * @date: Created by 2021/6/26 16:53
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.thread
 */
public class ScheduleThreadPoolExecutorTests {

    public static void main(String[] args) throws InterruptedException {
//        initScheduleThreadPoolExecutor();
//        testScheduleThreadPoolExecutorDelay();
//        testScheduleAtFixedRate();
//        scheduleWithFixedDelay();
//        scheduleWithFixedDelayException();
        scheduleWithFixedDelayCancel();
    }

    //初始化一个调度任务线程池
    public static void initScheduleThreadPoolExecutor() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
        scheduledExecutorService.schedule(() -> {
            System.out.println("ScheduledThreadPoolExecutor=============>");
            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + "任务处理完成");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }, 2, TimeUnit.SECONDS);
    }

    /**
     * 固定频率的调度线程池
     */
    public static void testScheduleAtFixedRate() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
        AtomicInteger count = new AtomicInteger(1);

        scheduledExecutorService.scheduleAtFixedRate(() -> {
            System.out.println("ScheduledThreadPoolExecutor=============>" + count.getAndIncrement());
            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + "任务处理完成");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }, 2,2, TimeUnit.SECONDS);
    }

    /**
     * 固定时间执行的调度线程池
     */
    public static void scheduleWithFixedDelay() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
        AtomicInteger count = new AtomicInteger(1);

        scheduledExecutorService.scheduleWithFixedDelay(() -> {
            System.out.println("ScheduledThreadPoolExecutor=============>" + count.getAndIncrement());
            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + "任务处理完成");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }, 2,5, TimeUnit.SECONDS);
    }


    /**
     * 调度线程池出异常的处理
     * @throws InterruptedException
     */
    public static void scheduleWithFixedDelayException() throws InterruptedException {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
        AtomicInteger count = new AtomicInteger(1);

        ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleWithFixedDelay(() -> {
            System.out.println("ScheduledThreadPoolExecutor=============>" + count.getAndIncrement());
            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + "任务处理完成");
                int i = 10 / 0;
                System.out.println(i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }, 2, 5, TimeUnit.SECONDS);

        TimeUnit.SECONDS.sleep(5);
        System.out.println("是否被取消：" + scheduledFuture.isCancelled());
        System.out.println("是否执行完成：" + scheduledFuture.isDone());

    }


    /**
     * 手动取消线程池
     * @throws InterruptedException
     */
    public static void scheduleWithFixedDelayCancel() throws InterruptedException {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
        AtomicInteger count = new AtomicInteger(1);

        ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleWithFixedDelay(() -> {
            System.out.println("ScheduledThreadPoolExecutor=============>" + count.getAndIncrement());
            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + "任务处理完成");

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }, 2, 5, TimeUnit.SECONDS);

        TimeUnit.SECONDS.sleep(5);
        scheduledFuture.cancel(false);
        TimeUnit.SECONDS.sleep(2);
        System.out.println("是否被取消：" + scheduledFuture.isCancelled());
        System.out.println("是否执行完成：" + scheduledFuture.isDone());

    }
}
